
#import "TorrentFileListViewController.h"

@implementation TorrentFileListViewController

//
// torrent info
//
@synthesize torrentInfo														= _torrentInfo;

//
// file list
//
@synthesize fileList														= _fileList;

//
// init
//
- (id)init
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup title
	//
	self.title																= NSLocalizedStringFromTable(@"File List", @"TorrentFileListViewController", nil);
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.torrentInfo														= nil;
	self.fileList															= nil;
	
	[super dealloc];
}

//
// refresh file list
//
- (void)refreshFileList:(id)sender
{
	//
	// get list
	//
	NexusClient* client														= [NexusClient sharedClient];
	id getTorrentFileListFinished = ^void(GetTorrentFileListRequest* httpRequest)
	{
		[ActivityView removeView];
		if(!httpRequest.error && httpRequest.response.statusCode == 200)
		{
			self.fileList													= httpRequest.fileList;
			for(TorrentFileInfo* info in self.fileList)
				[[DataCenter sharedDataCenter] addFileInfo:info forTorrentId:self.torrentInfo.torrentId];
			
			[self.tableView reloadData];
			self.navigationItem.rightBarButtonItem							= nil;
		}
		else
		{
			NSString* message												= [httpRequest errorDescription];
			NSString* title													= NSLocalizedStringFromTable(@"Get File List Failed", @"TorrentFileListViewController", nil);
			[UIAlertView alertViewWithTitle:title message:message cancel:NSLocalizedString(@"OK", nil)];
		}
	};
	[ActivityView showActivityView:NSLocalizedStringFromTable(@"Getting File List", @"TorrentFileListViewController", nil) networking:YES];
	[client getTorrentFileList:self.torrentInfo.torrentId callback:getTorrentFileListFinished];
}

//
// did load
//
- (void)viewDidLoad
{
	[super viewDidLoad];
	NSArray* fileList														= [[DataCenter sharedDataCenter] fileListForTorrentId:self.torrentInfo.torrentId];
	if(fileList)
	{
		self.fileList														= fileList;
	}
	else
	{
		self.navigationItem.rightBarButtonItem								= [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshFileList:)] autorelease];
		[self refreshFileList:nil];
	}
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get title
//
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
	return self.torrentInfo.title;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.fileList.count;
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	TorrentFileListViewCell* theCell										= [tableView dequeueReusableCellWithIdentifier:@"s"];
	if(!theCell)
		theCell																= [[[TorrentFileListViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"s"] autorelease];
	
	TorrentFileInfo* fileInfo												= [self.fileList objectAtIndex:indexPath.row];
	theCell.fileNameLabel.text												= fileInfo.fileName;
	theCell.fileSizeLabel.text												= fileInfo.fileSize;
	theCell.accessoryType													= UITableViewCellAccessoryNone;
	theCell.selectionStyle													= UITableViewCellSelectionStyleNone;
	return theCell;
}

//
// get height
//
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
	TorrentFileInfo* fileInfo												= [self.fileList objectAtIndex:indexPath.row];
	return [TorrentFileListViewCell rowHeightForFileName:fileInfo.fileName fileSize:fileInfo.fileSize maxWidth:self.tableView.bounds.size.width - 20];
}

@end
